#include "modelnode.h"

namespace HIM {
ModelNode::ModelNode(osg::Node * model):
    tr_{new osg::MatrixTransform()}
{
    tr_->setMatrix(osg::Matrix::scale(modelSize,modelSize,modelSize));
    model_=model;
    moveModel(50,-55,0);
    //设置模型颜色为散射光，也就是模型自身的颜色
    //auto ss= model->getOrCreateStateSet();
    //ss->setMode(GL_BLEND,osg::StateAttribute::ON);
    //osg::ref_ptr<osg::Material> mat = new osg::Material;
    //漫发射光
    //mat->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,1));
    //环境光
    //mat->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0,1.0,1.0,1));
    //设置材质的混合光颜色
    //mat->setTransparency(osg::Material::FRONT_AND_BACK,0.8);
    //ss->setAttributeAndModes(mat,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
    //ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
    tr_->addChild(model_);
}

void ModelNode::setModelTransparency(float transparency)
{
    //设置模型颜色为散射光，也就是模型自身的颜色
    auto ss= model_->getOrCreateStateSet();
    osg::ref_ptr<osg::Material> mat = new osg::Material;
    mat->setTransparency(osg::Material::FRONT_AND_BACK,transparency);
    ss->setAttributeAndModes(mat,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
}

void ModelNode::moveModel(float x,float y,float z)
{
    tr_->setMatrix(osg::Matrix::scale(modelSize,modelSize,modelSize)*osg::Matrix::translate(x,y,z));
    modelPosition = osg::Vec3(x, y, z);
}

void ModelNode::rotateModel(double angle , double x , double y , double z)
{
    tr_->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(angle),x,y,z));
}

void ModelNode::scalingModel(float size)
{
    tr_->setMatrix(osg::Matrix::scale(size,size,size));
    modelSize = size;
}

void ModelNode::setModelMask(bool show)
{
    tr_->setNodeMask(show);
}

}
